package com.bdqn.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.crm.pojo.BasDict;
import com.bdqn.crm.service.BasDictService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
public class BasDictController {
    @Resource
    private BasDictService basDictService;
    @RequestMapping(value = "dict/list")
    public String list(Model model,Long dictId, String dictItem, Long pageIndex){
        //数据回显
        model.addAttribute("dictId",dictId);
        model.addAttribute("dictItem",dictItem);
        //如果为空，查询第一页
        if (pageIndex == null){
            pageIndex = 1L;
        }
        Page<BasDict> page = new Page<>();
        page.setCurrent(pageIndex);
        IPage<BasDict> basDictIPage = basDictService.page(dictId,dictItem,page);
        model.addAttribute("dictPage",basDictIPage);
        return "dict/list";
    }

    /**
     * 跳转添加页面
     * @return
     */
    @GetMapping(value = "/dict/add")
    public String add(){
        return "/dict/add";
    }

    /**
     * 去到编辑页面去修改
     * @param dictId
     * @param model
     * @return
     */
    @GetMapping(value = "dict/edit")
    public String edit(Long dictId,Model model){
        BasDict basDict = basDictService.selectId(dictId);
        model.addAttribute("dict",basDict);
        return "dict/edit";
    }
    /**
     * 新增或修改
     * @param basDict
     * @return
     */
    @PostMapping("/dict/save")
    public String save(BasDict basDict){
        basDictService.save(basDict);
        return "redirect:/dict/list";
    }

    @ResponseBody
    @GetMapping(value = "dict/del/{dictId}")
    public Map del(Long dictId){
        basDictService.removeById(dictId);
        Map map = new HashMap();
        map.put("delResult","true");
        return map;
    }

}
